package com.yanqu.road.dao.impl.activity.dafuweng;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.dafuweng.DFWBossData;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DFWBossDataDaoImpl extends TempDataObjectDao<DFWBossData> {

    public static String getDFWBossDataSql = "select * from t_c_activity_dfw_boss where activityId=? and groupId=?";

    public Map<Integer, Map<Integer, DFWBossData>> getDFWBossDataMap(int activityId, int groupId) {
        Map<Integer, Map<Integer, DFWBossData>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getDFWBossDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                DFWBossData dFWBossData = parse(rs);
                if (!map.containsKey(dFWBossData.getBossType())) {
                    map.put(dFWBossData.getBossType(), new ConcurrentHashMap<>());
                }
                map.get(dFWBossData.getBossType()).put(dFWBossData.getBossId(), dFWBossData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateDFWBossDataSql = "update t_c_activity_dfw_boss set bossBatch=?,bossType=?,totalHp=?,curHp=?,killerUserId=?,killerPlayer=?,killTime=? where activityId=? and groupId=? and bossId=?";

    @Override
    protected boolean updateData(DFWBossData dFWBossData) {
        dFWBossData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, dFWBossData.getBossBatch()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWBossData.getBossType()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWBossData.getTotalHp()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWBossData.getCurHp()));
        para.put(i++, new DbParameter(Types.BIGINT, dFWBossData.getKillerUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(dFWBossData.getKillerPlayer())));
        para.put(i++, new DbParameter(Types.BIGINT, dFWBossData.getKillTime()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWBossData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWBossData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWBossData.getBossId()));
        if (getDb().executeNoneQuery(updateDFWBossDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertDFWBossDataSql = "insert into t_c_activity_dfw_boss(activityId,groupId,bossId,bossBatch,bossType,totalHp,curHp,killerUserId,killerPlayer,killTime) values (?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(DFWBossData dFWBossData) {
        dFWBossData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, dFWBossData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWBossData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWBossData.getBossId()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWBossData.getBossBatch()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWBossData.getBossType()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWBossData.getTotalHp()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWBossData.getCurHp()));
        para.put(i++, new DbParameter(Types.BIGINT, dFWBossData.getKillerUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(dFWBossData.getKillerPlayer())));
        para.put(i++, new DbParameter(Types.BIGINT, dFWBossData.getKillTime()));
        if (getDb().executeNoneQuery(insertDFWBossDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected DFWBossData parseData(ResultSet rs) throws SQLException {
        DFWBossData dFWBossData = new DFWBossData();
        dFWBossData.setActivityId(rs.getInt("activityId"));
        dFWBossData.setGroupId(rs.getInt("groupId"));
        dFWBossData.setBossId(rs.getInt("bossId"));
        dFWBossData.setBossBatch(rs.getInt("bossBatch"));
        dFWBossData.setBossType(rs.getInt("bossType"));
        dFWBossData.setTotalHp(rs.getInt("totalHp"));
        dFWBossData.setCurHp(rs.getInt("curHp"));
        dFWBossData.setKillerUserId(rs.getLong("killerUserId"));
        dFWBossData.setKillerPlayer(JSONObject.parseObject(rs.getString("killerPlayer"), new TypeReference<UserBaseInfo>() {
        }));
        dFWBossData.setKillTime(rs.getLong("killTime"));
        return dFWBossData;
    }

}